﻿Public Class FrmHandover

    Private x_handover As X_Handover
    Private x_handover_old As X_Handover

    Private _parentObj As PO
    Private _parentForm As SuperForm

    Public Sub New(ByVal xhandover As X_Handover, ByVal parentObj As PO, ByVal parentForm As SuperForm)
        InitializeComponent()

        Me._parentObj = parentObj
        If (Not Me._parentObj Is Nothing) Then
            Me._parentObj.setBehave(CONSTANT.BEHAVE_TYPE._UPDATE)
        End If

        Me._parentForm = parentForm

        Me.x_handover = xhandover
        If (Not parentObj Is Nothing) Then
            Me.x_handover.setValue(I_Handover.ColumnName_MaTS, Me._parentObj.getValue(I_TSCD.ColumnName_MaSO))
            Me.x_handover.setValue(I_Handover.ColumnName_parentID, Me._parentObj.getValue(I_TSCD.ColumnName_id))
            Me.x_handover.setValue(I_Handover.ColumnName_FAType, Me._parentObj.getValue(I_TSCD.ColumnName_FAType))
            Me.x_handover.setValue(I_Handover.ColumnName_Status, CONSTANT.STATUS._DRAFTS)
        End If

        Me.x_handover_old = Me.x_handover

        ini()
    End Sub

    Private Function ini() As Boolean

        Me.txtNguoiGiao.Text = Me.x_handover_old.getValue(I_Handover.ColumnName_NguoiGiao)
        Me.txtNguoiNhan.Text = Me.x_handover_old.getValue(I_Handover.ColumnName_NguoiNhan)
        Me.dtpThoiGian.Text = Me.x_handover_old.getValue(I_Handover.ColumnName_ThoiGian)
        Me.cbDiaDiem.Text = Me.x_handover_old.getValue(I_Handover.ColumnName_DiaDiem)
        Me.cbLoaiBanGiao.Text = Me.x_handover_old.getValue(I_Handover.ColumnName_LoaiBanGiao)

        If (Not Me._parentObj Is Nothing) Then
            Select Case Me._parentObj.getValue(I_TSCD.ColumnName_IsBusy)
                Case 0
                    Me.cbLoaiBanGiao.Items.Add("Bàn giao - Mượn")
                    Me.cbLoaiBanGiao.SelectedIndex = 0
                Case 1
                    Me.cbLoaiBanGiao.Items.Add("Bàn giao - Trả")
                    Me.cbLoaiBanGiao.SelectedIndex = 0
            End Select
        End If
        Return True
    End Function


    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub


    Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ini()
    End Sub


    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Me.x_handover.setValue(I_Handover.ColumnName_NguoiGiao, Me.txtNguoiGiao.Text)
        Me.x_handover.setValue(I_Handover.ColumnName_NguoiNhan, Me.txtNguoiNhan.Text)
        Me.x_handover.setValue(I_Handover.ColumnName_ThoiGian, Me.dtpThoiGian.Text)
        Me.x_handover.setValue(I_Handover.ColumnName_DiaDiem, Me.cbDiaDiem.Text)
        Me.x_handover.setValue(I_Handover.ColumnName_LoaiBanGiao, Me.cbLoaiBanGiao.Text)

        Me.x_handover.save(True)

        If (Not Me._parentObj Is Nothing) Then
            Dim isBusy = Me._parentObj.getValue(I_TSCD.ColumnName_IsBusy)
            Me._parentObj.setValue(I_TSCD.ColumnName_IsBusy, 1 - isBusy)
            Me._parentObj.save(True)
        End If

        Me._parentForm.loadFA()
        Me._parentForm.loadHandover()
        Me._parentForm.specifyCorrespondingHandover()

        Me.Close()

    End Sub
End Class